package com.zdft.promotion.common.util;

import com.zdft.promotion.common.dao.ds.DS;
import com.zdft.promotion.common.dao.ds.DSRoute;
import com.zdft.promotion.common.dao.ds.DataSourceContextHolder;
import oracle.jdbc.OracleTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

@Service
@DS(DSRoute.ERP)
@Deprecated
public class BusinessUtils {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    static JdbcTemplate sJdbcTemplate;

    @PostConstruct
    public void init(){
        sJdbcTemplate=jdbcTemplate;
    }
    /**
     * 调用存储过程
     * 返回整型 主键
     */
    public static Integer getPK(final String tableName) {
        Integer iRet = (Integer) sJdbcTemplate.execute(
                new CallableStatementCreator() {
                    public CallableStatement createCallableStatement(
                            Connection con) throws SQLException {
                        String warehousedProc = "{call get_seq(?,?)}";// 调用的sql
                        CallableStatement cs = con.prepareCall(warehousedProc);
                        cs.setString(1, tableName);// 设置输入参数的值
                        cs.registerOutParameter(2, OracleTypes.INTEGER);// 注册输出参数的类型
                        return cs;
                    }
                }, new CallableStatementCallback() {
                    public Object doInCallableStatement(CallableStatement cs)
                            throws SQLException, DataAccessException {
                        cs.execute();
                        return cs.getInt(2);// 获取输出参数的值
                    }
                });
        return iRet;
    }

    public static String getEanCode(String tableName, long length) throws Exception {
        Long id =null;
        try{
            DataSourceContextHolder.setDB(DSRoute.ERP);
            id = getPK(tableName).longValue();
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            DataSourceContextHolder.clearDB();
        }
        String eanCode = getVerifyBit(id, length);
        return eanCode;
    }

    private static String getVerifyBit(Long szCode, long length) throws Exception {
        String strCode = String.valueOf(szCode);
        if (String.valueOf(szCode) == null || String.valueOf(szCode).length() > length) {
            throw new Exception("要编码的字符串为空或者长度和要求得到的长度不合规范！");
        }
        for (int i = 0; i < length - 1 - String.valueOf(szCode).length(); i++) {
            strCode = "0" + strCode;
        }
        int nNum1 = 0;
        int nNum2 = 0;
        int nNum3 = 0;
        for (int i = strCode.length() - 1; i >= 0; i--) {
            if (i % 2 == 0) {
                nNum1 += Integer.parseInt(String.valueOf(strCode.charAt(i)));// 奇数
            } else {
                nNum2 += Integer.parseInt(String.valueOf(strCode.charAt(i)));
                // 偶数
            }
        }
        nNum3 = nNum1 + nNum2 * 3;
        int t;
        if (nNum3 % 10 > 0)
            t = 10 - (nNum3 % 10);
        else
            t = 0;
        strCode += t;
        return strCode;
    }

}
